<template>
    <div>
        <div class="container">
            <el-page-header @back="goBack" content="客户组/学校回收汇总"></el-page-header>

            <div class="e-p-b-15 e-p-t-45">
                <el-radio-group v-model="dataType" class="e-m-b-20">
                    <el-radio-button label="0">数据中台</el-radio-button>
                    <el-radio-button label="1">库存表</el-radio-button>
                    <el-radio-button label="2">学校回收情况</el-radio-button>
                    <el-radio-button label="3">入库支出</el-radio-button>
                    <el-radio-button label="4">回收汇总</el-radio-button>
                </el-radio-group>
                <div>
                    <el-radio-group v-model="product" class="e-m-b-45" size="mini">
                        <el-radio-button label="0">站点</el-radio-button>
                        <el-radio-button label="1">客户组/学校</el-radio-button>
                    </el-radio-group>
                </div>
                <div class="flex" style="border-bottom: 1px solid var(--light);">
                    <div class="e-flex-1">
                        <el-form ref="form" :model="form">
                            <el-form-item label="时间范围">
                            <el-radio-group v-model="form.timeType" size="small" @input="getList">
                                <el-radio-button label="0">全部</el-radio-button>
                                <el-radio-button label="1">今日</el-radio-button>
                                <el-radio-button label="2">昨日</el-radio-button>
                                <el-radio-button label="3">本月</el-radio-button>
                                <el-radio-button label="4">上月</el-radio-button>
                                <el-radio-button label="5">本季度</el-radio-button>
                                <el-radio-button label="6">上季度</el-radio-button>
                                <el-radio-button label="7">本年</el-radio-button>
                                <el-radio-button label="8">上年</el-radio-button>
                                <el-radio-button label="9">自定义日期</el-radio-button>
                            </el-radio-group>
                            </el-form-item>
                            <el-form-item label="自定义日期" v-if="form.timeType == 9">
                            <el-date-picker size="small" v-model="form.date" type="daterange" range-separator="至"
                                start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" @change="setTime"
                                :picker-options="expireTimeOPtion" :unlink-panels="true">
                            </el-date-picker>
                            </el-form-item>
                        </el-form>
                    </div>
                    <div>
                        <el-form :inline="true" class="demo-form-inline xuan-box">
                            <el-form-item label="客户组/学校">
                                <el-select 
                                    v-model="eid" 
                                    placeholder="选择客户组/学校" 
                                    clearable
                                    filterable
                                    remote
                                    :remote-method="geEnterprisetList"
                                    :loading="loading"
                                    @change="getList"
                                >
                                    <el-option v-for="item in enterpriseList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="关键词">
                                <el-input placeholder="请输入" v-model="key" prefix-icon="el-icon-search" clearable></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="getList()">查询</el-button>
                            </el-form-item>
                            <el-form-item>
                            <el-button type="primary" @click="downloadwithdrawa()" plain><i
                                class="iconfont icon-excel"></i>导出Excel</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                </div>
            </div>

            <div>
                <el-table
                    :data="tableData"
                    border
                    :span-method="objectSpanMethod"
                    style="width: 100%">
                    <el-table-column
                        prop="name"
                        label="客户组/学校"
                        >
                    </el-table-column>
                    <el-table-column
                        v-if="eid > 0"
                        prop="department"
                        label="部门/班级"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="weight"
                        label="回收重量（KG）"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="quantity"
                        label="回收件数（件）"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="amount"
                        label="回收金额（元）"
                        >
                    </el-table-column>

                    <!-- <el-table-column
                        prop="pweight"
                        label="回收总重量（KG）"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="pquantity"
                        label="回收总件数（件）"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="pamount"
                        label="回收总金额（元）"
                        >
                    </el-table-column> -->
                </el-table>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    layout: 'admin',
    head() {
        return {
            title: '客户组/学校回收汇总' 
        }
    },
    data() {
        return {
            dataType: 4,
            product: 1,
            key: '',
            eid: null,
            loading: false,
            enterpriseList: [],
            form: {
                timeType: 3,
                date: []
            },
            tableData: [],
            expireTimeOPtion: {
                disabledDate(time) {
                    return time.getTime() > Date.now() - 8.64e6;  //如果没有后面的-8.64e6就是不可以选择今天的 
                }
            }
        }
    },
    mounted() {
        this.getList()
        this.geEnterprisetList()
    },
    watch: {
        dataType(val) {
            if (val == 0) {
                this.$router.push({
                    path: '/'
                })
            } else if (val == 1) {
                this.$router.push({
                    path: '/inventory/list'
                })
            }
            else if (val == 2) {
                this.$router.push({
                    path: '/inventory/schoollist'
                })
            }
            else if (val == 3) {
                this.$router.push({
                    path: '/finance/receipt/receipt'
                })
            } else if (val == 4) {
                this.$router.push({
                    path: '/shop/stockTable'
                })
            }
        },
        product(val) {
            console.log(val)
            if (val == 0) {
                this.$router.push({
                    path: '/shop/stockTable'
                })
            } else if (val == 1) {
                this.$router.push({
                    path: '/enterprise/stockTable'
                })
            }
        }
    },
    methods: {
        goBack() {
            this.$router.go(-1)
        },
        getList(){
            // 获取站点回收量汇总
            let param = {
                enterpriseId: this.eid || 0,
                key: this.key,
                timeType: this.form.timeType
            }
            if (this.form.date !== null) {
                if (this.form.date.length > 0) {
                    param.startDate = this.form.date[0]
                    param.endDate = this.form.date[1]
                }
            }
            this.$axios.post('/User/Order/SumByEnterprise',param).then((res)=>{
                // let data = []
                // res.forEach((item,index)=>{
                //     item.list.forEach((cell,i)=>{
                //         data.push({
                //             pid: item.index,
                //             amount: cell.amount,
                //             pname: item.name,
                //             department: cell.name,
                //             quantity: cell.quantity,
                //             weight: cell.weight,
                //             pamount: item.amount,
                //             pquantity: item.quantity,
                //             pweight: item.weight,
                //         })
                //     })
                // })
                this.tableData = res
            })
        },
        //合并类型格子
        objectSpanMethod({ row, column, rowIndex, columnIndex }) {
            let mergeLength = [0,5,6,7];//需要进行横纵合并的列,0开始
            
            for (let j = 0; j < mergeLength.length; j++) {

                if (columnIndex == mergeLength[j]) {
                    let finArray = [1, 1];
                    // 处理行数据
                    let cgname = Object.keys(row)[2] //合并单元格的主键依据，一般为第一列的值
                    if (rowIndex === 0 || row[cgname] !== this.tableData[rowIndex - 1][cgname]) {
                        let rowspan = 1;
                        //计算需要进行合并操作的行
                        for (let i = 0; i < this.tableData.length - 1; i++) {
                            // 只合并连续的
                            if (this.tableData[i][cgname] === row[cgname] && this.tableData[i + 1][cgname] === row[cgname]) {
                                rowspan++;
                            }
                        }
                        finArray[0] = rowspan;
                    } else {
                        finArray[0] = 0;
                    }
                    return finArray
                }
            }
        },
        //合并最后一个合计
        getSummaries(param) {
            const sums = []
            sums[0] = '合计'
            sums[1] = '/'
            // sums[2] = (this.myweight.online).toFixed(2) + 'KG' + ' / ' + this.piece.online + '件'
            
            // sums[3] = (this.myweight.offline).toFixed(2) + 'KG' + ' / ' + this.piece.offline + '件'
            // sums[4] = (this.myweight.total).toFixed(2) + 'KG' + ' / ' + this.piece.total + '件'
            // sums[6] = this.amount.online
            // sums[6] = this.amount.online
            // sums[7] = this.amount.offline
            // sums[8] = this.amount.total
            return sums;
            // sums数组的每一个元素代表表格从左到右的列(column)。
        },
        geEnterprisetList(key) {
            // 获取客户组选择下拉列表
            this.loading = true
            this.$axios.post('/Enterprise/List', {
                "PageIndex": 1,
                "PageSize": 999,
                "Key": key,
            }).then((res) => {
                this.enterpriseList = res.rows;
                // this.loading = false
            }).catch(()=>{
                // this.loading = false
            }).finally(()=>{
                console.log('获取客户组列表完毕')
                this.loading = false
            })
         },
        setTime(){
            console.log(this.form.date)
            // 选择自定义日期范围
            this.getList()
        },
        downloadwithdrawa() {
            //导出
            let param = {
                id: this.eid,
                key: this.key,
                timeType: this.form.timeType
            }
            if (this.form.date !== null) {
                if (this.form.date.length > 0) {
                    param.startDate = this.form.date[0]
                    param.endDate = this.form.date[1]
                }
            }
            this.$axios.post('/User/Order/SumByEnterpriseDownload', 
                param,
                {
                    responseType: 'blob'
                }
            ).then((res)=>{
                let content = res;
                let fileName = '客户组/学校回收汇总.xls'
                this.utils.saveAndExport(content, fileName)
            })
        }

    }
}
</script>

<style lang="scss" scoped>

</style>